<?php
/**
 * 单店基金-天网行动模型
 * @author jianhui
 *
 */
class singleNetModel extends MY_Model{
    function singleNetModel(){
        parent::MY_Model();
        $this->table = 'SingleNet';
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('singleProcessModel');
        $this->CI->singleProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['procId']) && $data['procId'] ? (int)$data['procId'] : 0;
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        $fileIds = array();
        foreach ($list as $v){
            if($v['fileId'] && !in_array($v['fileId'], $fileIds)){
                $fileIds[] = (int)$v['fileId'];
            }
        }
        //附件数据
        $this->CI->load->model('fileModel');
        $fileList = $this->CI->fileModel->getList(array('id'=>array('in',$fileIds)));
        //广告类型配置
        $netActionType = $this->config->item('netActionType');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            //广告类型
            $list[$k]['actionTypeStr'] = isset($netActionType[$v['actionType']]) ? $netActionType[$v['actionType']] : '';
            //预算审核状态
            $list[$k]['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($list[$k]['budgetAuditStr']){
                $list[$k]['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $list[$k]['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '';
            }
            //结算审核状态
            $list[$k]['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($list[$k]['settleAuditStr']){
                $list[$k]['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $list[$k]['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '';
            }
            //附件数据
            $list[$k]['fileData'] = isset($fileList[$v['fileId']]) ? $fileList[$v['fileId']] : array();
        }
        return $list;
    }
    /**
     * 获取天网行动列表
     * @param mix $where 查询条件
     * @param string $limit
     * @param string $offset
     * @param string $colum
     * @param string $orderby
     */
    function getListByProcId($where, $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $list = $this->getList($where, $limit, $offset, $colum, $orderby);
        $newList = array();
        foreach ($list as $v){
            $newList[$v['procId']] = $v;
        }
        return $newList;
    }
    /**
     * 更新天网行动金额
     * @param int $netId
     */
    function updateMoney($netId){
        $findData = $this->getData($netId);
        if (!$findData){
            return false;
        }
        //单店数据
        $this->CI->load->model('singleProcessModel');
        $procData = $this->CI->singleProcessModel->getData($findData['procId']);
        //报表金额
        $this->CI->load->model('importMoneyModel');
        $importMoney = $this->CI->importMoneyModel->getMoney($procData['storeId'], $procData['timeUnit'], 12, $procData['year']);
        $data['budgetApply'] = $importMoney;
        $data['budgetAudit'] = $importMoney;
        $data['settleApply'] = $findData['settleApply'];
        $data['settleAudit'] = $findData['settleAuditRs'] == 1 ? $data['settleApply'] : 0;
        $data['canSettleMoney'] = $findData['settleAuditRs'] == 1 ? min($data['budgetApply'], $data['settleApply'], $procData['canUseMoney']*0.3) : 0;
        $this->edit($netId, $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param int $procId
     * @return array
     */
    function getMoney($procId){
        $data = array(
            'budgetApply' => 0,//预算申请
            'budgetAudit' => 0,//预算审核
            'settleApply' => 0,//结算申请
            'settleAudit' => 0,//结算审核
            'canSettleMoney'  => 0,//可结算金额
            'buBudgetApply' => 0,//补报预算申请
            'buBudgetAudit' => 0,//补报预算审核
            'buSettleApply' => 0,//补报结算申请
            'buSettleAudit' => 0,//补报结算审核
            'buCanSettleMoney' => 0,//补报可结算金额
        );
        $findList = $this->getList(array('procId' => $procId));
        foreach ($findList as $v){
            if ($v['isBubao'] == 0){
                $data['budgetApply'] += $v['budgetApply'];
                in_array($v['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] += $v['budgetAudit'];
                if ($v['isSettle']){
                    $data['settleApply'] += $v['settleApply'];
                    in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['settleAudit'];
                }
                $data['canSettleMoney'] += $v['canSettleMoney'];
            }else{
                $data['budgetApply'] += $v['budgetApply'];
                in_array($v['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] += $v['budgetAudit'];
                if ($v['isSettle']){
                    $data['settleApply'] += $v['settleApply'];
                    in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['settleAudit'];
                }
                $data['buCanSettleMoney'] += $v['canSettleMoney'];
            }
        }
        return $data;
    }
    /**
     * 获取结算各状态数量
     * @param int $singleId
     * @param number $isBubao
     * @return multitype:number
     */
    function getSettleStatusCount($procId, $isBubao=0){
        $count = array(
            -1  => 0,//未提交
            0   => 0,//未审核
            1   => 0,//已通过
            2   => 0,//不通过
            3   => 0,//返回修改
            4   => 0,//部分通过
        );
        $findList = $this->getList(array('procId'=>$procId, 'isBubao'=>$isBubao, 'budgetAuditRs' => array('in',array(1,4))));
        foreach ($findList as $v){
            if (isset($count[$v['settleAuditRs']])){
                $count[$v['settleAuditRs']] ++;
            }
        }
        return $count;
    }
    /**
     * 结算单个审核
     * @param number $id
     * @param number $audit
     * @param array $user
     * @return object
     */
    function settleAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'settleAudit' =>  $audit==1 ? $findData['settleApply'] : 0,
            'settleAuditRs' => $audit,
            'settleAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
}